package platform;

import common.Constant;
import common.Singleton;
import operator.Handler;
import pri.swg.Fader;
import vo.Mean;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

/**
 * 负责选择位置和意象的面板
 *
 * @author 柴家琪
 */

@SuppressWarnings("serial")
public class SelectLabel extends JLabel {
    private static final int CombineToMain = 1, DeleteLocation = 2;


    private SelectLabel(String text) {

        setFont(new Font(Constant.boldfaceName, Font.BOLD, 14));
        setPreferredSize(new Dimension(10 + text.length() * 16, 25));
        setText(text);
        setHorizontalAlignment(JLabel.CENTER);
        setOpaque(true);
        setForeground(Color.WHITE);
        setBorder(new LineBorder(Color.white, 2));
    }

    private SelectLabel() {
        this("关闭选择面板");
        setBackground(Color.RED);
        Fader.fade(this, Fader.FORE_GROUND, 300, Fader.LISTEN_MouseEnter, Color.RED);
        Fader.fade(this, Fader.BACK_GROUND, 200, Fader.LISTEN_MouseEnter, Color.white);
        addMouseListener(new MouseAdapter() {
            public void mouseReleased(MouseEvent e) {
                // handler.vc.callBackSelecter();//召回
                Singleton.vc().callBackAll();
            }
        });
    }

    // 选择面板，显示位置（删除位置时）和主意象（归并意象时）
    private SelectLabel(String text, int from, MeanLabel mLabel, Mean mean, int type) {
        this(text);
        Handler handler = Singleton.handler();

        if (type == CombineToMain) {
            setBackground(Color.orange);
            Fader.fade(this, Fader.FORE_GROUND, 300, Fader.LISTEN_MouseEnter, Color.ORANGE);
            addMouseListener(new MouseAdapter() {
                public void mouseEntered(MouseEvent e) {
                    mLabel.setBackground(Color.WHITE);
                }

                public void mouseExited(MouseEvent e) {
                    mLabel.setBackground(Color.orange);
                }

                public void mouseReleased(MouseEvent e) {
                    handler.combineTo(mean);
                    // handler.vc.callBackSelecter();
                    Singleton.vc().callBackAll();
                }
            });
        }
        Fader.fade(this, Fader.BACK_GROUND, 200, Fader.LISTEN_MouseEnter, Color.white);
    }

    public static SelectLabel[] allMeanLabel() {
        ArrayList<SelectLabel> as = new ArrayList<>();
        for (Mean next : Singleton.handler().mainMean) {
            if (next.getSuitPage() == Singleton.main().presentPage || Singleton.main().presentPage == 0) {
                as.add(new SelectLabel(next.getText(), 0, next.getLabel(), next, CombineToMain));
            }
        }
        as.add(new SelectLabel());
        SelectLabel all[] = new SelectLabel[as.size()];
        return as.toArray(all);
    }

}
